package com.wfcm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.wfcm.annotation.IgnoreSign;
import com.wfcm.annotation.IgnoreToken;
import com.wfcm.entity.WfMemberEntity;
import com.wfcm.entity.WfNewMessageEntity;
import com.wfcm.service.WfMemberService;
import com.wfcm.service.WfNewMessageService;
import com.wfcm.utils.DateUtil;
import com.wfcm.utils.ISsoLoginHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wfcm.entity.WfMemberCouponsEntity;
import com.wfcm.service.WfMemberCouponsService;
import com.wfcm.utils.R;
import com.wfcm.utils.WebUtils;


/**
 * @content 优惠券Controller
 */
@RequestMapping("/coupon")
@Controller
public class CouponController {
	private Logger log = LoggerFactory.getLogger(AdInfoController.class);
	@Autowired
	WfMemberCouponsService memberCouponsService;
	@Autowired
	private WfMemberService memberService;
    @Autowired
    private WfNewMessageService newMessageService;
	
	/**
	 * 优惠券展示接口
	 */
	@RequestMapping("/coupons")
	@ResponseBody
	public R goods(HttpServletRequest req) {
		Integer userId = ISsoLoginHelper.confirm(req);
		String status = req.getParameter("status");
		String out = req.getParameter("out");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("status", status);
		map.put("out", out);//是否过期 （1，是；2，否）
		
		List<WfMemberCouponsEntity> resultList = memberCouponsService.queryList(map);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("resultList", resultList);
		
		return R.ok(resultMap);
	}

	/**
	 * 领取优惠券
	 */
	@RequestMapping("/getCoupons")
	@ResponseBody
	public R getCoupons(HttpServletRequest req) {
		Integer uid = ISsoLoginHelper.confirm(req);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", uid);
		map.put("out", 2);//是否过期 （1，是；2，否）
		map.put("source", 2);//优惠活动
		map.put("title", "满2000减300");//
		List<WfMemberCouponsEntity> resultList = memberCouponsService.queryList(map);
		if (resultList.size()==0) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date beginDate = null;
			Date endDate = null;
			try {
				beginDate =  formatter.parse("2018-08-27 20:00:00");
				endDate =  formatter.parse("2018-09-03 23:00:00");
			} catch (ParseException e) {
				e.printStackTrace();
			}
			//奖励大语文类优惠券
			WfMemberCouponsEntity memberCouponsEntity = new WfMemberCouponsEntity();
			memberCouponsEntity.setUserid(uid);
			memberCouponsEntity.setTitle("满2000减300");
			memberCouponsEntity.setCost(300);
			memberCouponsEntity.setFtype(2000);
			memberCouponsEntity.setLimits(1);
			memberCouponsEntity.setBeginDate(beginDate);
			memberCouponsEntity.setEndDate(endDate);
			memberCouponsEntity.setUpdateTime(new Date());
			memberCouponsEntity.setStatus(2);
			memberCouponsEntity.setCouponsSource(2);//活动领取
			memberCouponsEntity.setReceiveTime(new Date());
			memberCouponsEntity.setValueIds("");
			memberCouponsService.save(memberCouponsEntity);
			memberCouponsEntity.setUserid(uid);
			memberCouponsEntity.setTitle("满1200减150");
			memberCouponsEntity.setCost(150);
			memberCouponsEntity.setFtype(1200);
			memberCouponsEntity.setLimits(1);
			memberCouponsEntity.setBeginDate(beginDate);
			memberCouponsEntity.setEndDate(endDate);
			memberCouponsEntity.setUpdateTime(new Date());
			memberCouponsEntity.setStatus(2);
			memberCouponsEntity.setCouponsSource(2);//活动领取
			memberCouponsEntity.setReceiveTime(new Date());
			memberCouponsEntity.setValueIds("");
			memberCouponsService.save(memberCouponsEntity);
			memberCouponsEntity.setUserid(uid);
			memberCouponsEntity.setTitle("满800减80");
			memberCouponsEntity.setCost(80);
			memberCouponsEntity.setFtype(800);
			memberCouponsEntity.setLimits(1);
			memberCouponsEntity.setBeginDate(beginDate);
			memberCouponsEntity.setEndDate(endDate);
			memberCouponsEntity.setUpdateTime(new Date());
			memberCouponsEntity.setStatus(2);
			memberCouponsEntity.setCouponsSource(2);//活动领取
			memberCouponsEntity.setReceiveTime(new Date());
			memberCouponsEntity.setValueIds("");
			memberCouponsService.save(memberCouponsEntity);
			return R.ok();
		}else {
			return R.error(71, "您已经领取过了！");
		}
	}

	@RequestMapping("saveCoupons")
	@ResponseBody
	@IgnoreToken
	@IgnoreSign
	public void saveCoupons(){
		String tels = "13604400010,13248049088,15094562983,13146509307";
		String[] typeArr = tels.split(",");
		List<String> typeList1 = Arrays.asList(typeArr);//数组转为list
		List<String> typeList = new ArrayList(typeList1);
		Date beginDate = new Date();
        String string = "2019-04-01 22:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endDate = null;
        try {
            endDate = sdf.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < typeList.size(); i++) {
			WfMemberCouponsEntity memberCouponsEntity = new WfMemberCouponsEntity();
			//判断用户是否存在,若存在加入增加微信标识即可
			WfMemberEntity memberEntity =  memberService.queryByMobile(typeList.get(i));
			if(memberEntity != null) {
				memberCouponsEntity.setUserid(memberEntity.getUserid());
				memberCouponsEntity.setTitle("50元抵扣券");
				memberCouponsEntity.setCost(50);
				memberCouponsEntity.setFtype(0);
				memberCouponsEntity.setLimits(1);
				memberCouponsEntity.setBeginDate(beginDate);
				memberCouponsEntity.setEndDate(endDate);
				memberCouponsEntity.setUpdateTime(new Date());
				memberCouponsEntity.setStatus(2);
				memberCouponsEntity.setCouponsSource(2);//活动领取
				memberCouponsEntity.setReceiveTime(new Date());
				memberCouponsEntity.setValueIds(null);
				memberCouponsService.save(memberCouponsEntity);

                /*WfNewMessageEntity newMessageEntity  = new WfNewMessageEntity(memberEntity.getUserid(),null,2,0,"优惠券获取提醒","恭喜您获取了50元的全场抵扣券！！！",new Date(),null,"https://wftest.wfkids.net/wfcm-api/msg-activity.html",
                        "");
                newMessageService.save(newMessageEntity);*/
			}
		}

	}

}
